package com.users.dao;

import java.util.List;

import com.common.NomalHibernateDaoSupport;
import com.users.entity.Users;

public class UsersDaoImpl extends NomalHibernateDaoSupport implements IUsersDao {

	@Override
	public void delete(Integer id) {
		getHibernateTemplate().delete(get(id));
	}

	@Override
	public void delete(Users users) {
		getHibernateTemplate().delete(users);
	}

	@Override
	public List<Users> findAllUsers(Integer indexPage) {
		return (List<Users>)this.findByPage("from Users",indexPage, 1);
	}

	public Integer findAllUsers() {
		return getHibernateTemplate().find("from Users").size();
	}

	@Override
	public List<Users> findByName(String name,Integer indexPage) {
		return (List<Users>)this.findByPage("from Users u where u.username like ?","%"+name+"%",indexPage, 1);
	}
	
	public Integer findByName(String name) {
		return getHibernateTemplate().find("from Users u where u.username like ?","%"+name+"%").size();
	}
	
	@Override
	public List<Users> findByUsers(String name, String password) {
		Object[] value={name,password};
		return (List<Users>)getHibernateTemplate().find("from Users u where u.username = ? and u.password = ?",value);
	}

	@Override
	public Users get(Integer id) {
		return (Users)getHibernateTemplate().get(Users.class, id);
	}

	@Override
	public long getUsersNumber() {
		return (Long)getHibernateTemplate().find("select count(*) from Users as u").get(0);
	}

	@Override
	public Integer save(Users users) {
		return (Integer)getHibernateTemplate().save(users);
	}

	@Override
	public void update(Users users) {
		getHibernateTemplate().update(users);
	}

}
